package com.saycode.back.article.controller;


import com.saycode.back.article.service.ArticleService;
import com.saycode.back.articleType.service.ContentTypeService;
import com.saycode.common.Constant;
import com.saycode.common.MethodUtil;
import com.saycode.common.beans.Article;
import com.saycode.common.beans.ArticleContent;
import com.saycode.common.beans.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyanhui on 2017/6/14.
 */
@Controller("back/article")
@RequestMapping("back/article")
public class ArticleController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    @Qualifier("back/articleService")
    private ArticleService articleService;

    @Autowired
    @Qualifier("back/contentTypeService")
    private ContentTypeService contentTypeService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(ModelMap map, HttpSession session, @Param("pageNum") Integer pageNum) {
        User user = (User)session.getAttribute(Constant.USER_SESSION_KEY);
        if(pageNum == null) pageNum = 1;
        Page<Article> page = articleService.findByUserId(user.getId(),pageNum);
        List<Article> listArticle = page.getContent();

        map.put("hasNext",page.getTotalPages() > pageNum);
        map.put("hasPrev",pageNum > 1);
        map.put("pageNum",pageNum);
        map.put("articleList",listArticle);
        return "/back/article/list";
    }
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        map.put("contentTypeList",contentTypeService.findAll());
        return "/back/article/add";
    }
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(ModelMap map, @RequestParam("id") Long id) {
        map.put("article",articleService.findById(id));
        map.put("content",articleService.findContentById(id));
        map.put("contentTypeList",contentTypeService.findAll());
        return "/back/article/edit";
    }

    /**
     * 保存文章数据 包含ID执行修改，不包含ID执行新增
     * @param article
     * @param session
     * @param request
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(Article article,HttpSession session, HttpServletRequest request) {
        String content = request.getParameter("content");
        if (article.getId() == null) {//新增
            Long id = MethodUtil.getId();
            article.setId(id);
            User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
            article.setUserId(user.getId());
            article.setRealName(user.getRealName());
            article.setPhotoUrl(user.getPhotoUrl());
            article.setCreateDate(new Date());
            articleService.save(article,content);
        }else{ //修改
            Article articleDB = articleService.findById(article.getId());
            articleDB.setTitle(article.getTitle());
            articleDB.setContentType(article.getContentType());
            articleService.save(articleDB,content);
        }
        logger.info(content);
        return "ok";
    }
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    @ResponseBody
    public String updateState(Article article) {
        logger.info(article.getId()+"");
        Article articleDB = articleService.findById(article.getId());
        logger.info(articleDB.getRealName());
        articleDB.setStatus(article.getStatus());
        articleService.save(articleDB);
        return "ok";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestParam("id") Long id) {
        articleService.delete(id);
        return "ok";
    }
}
